package com.darren.befriend;

import java.util.ArrayList;
import java.util.Stack;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.darren.befriend.constants.Constant;
import com.darren.befriend.fragments.FilmFragment;
import com.darren.befriend.fragments.FriendForFragment;
import com.darren.befriend.fragments.HobbyBaseFragment;
import com.darren.befriend.fragments.HobbyBaseFragment.checkHobiesListener;
import com.darren.befriend.fragments.InterestFragment;
import com.darren.befriend.fragments.MusicFragment;
import com.darren.befriend.fragments.SportsFragment;
import com.darren.befriend.fragments.UniversityFragment.OnUniversitySelected;
import com.darren.befriend.fragments.UserSettingFragment;
import com.darren.befriend.models.HobbyModel;
import com.darren.befriend.utilities.LogUtil;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class UserSettingInitialActivity extends BaseActivity implements
		OnClickListener, checkHobiesListener, OnUniversitySelected {
	private ImageView mHeaderLeftBtn;
	private ImageView mHeaderRightBtn;
	private TextView mHeaderTitle;
	private FragmentManager mFragmentManager;
	private Stack<String> mTitleStack;
	protected ArrayList<HobbyModel> selectionArray;
	private FragmentTransaction ft;
	private String FRAGMENT_TAG = "fragment";
	private Fragment newFragment = null;

	@Override
	public void onCreate(Bundle arg0) {

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(arg0);
		setContentView(R.layout.activity_user_setting_initation);
		mHeaderLeftBtn = (ImageView) findViewById(R.id.header_left_btn);
		mHeaderLeftBtn.setOnClickListener(this);
		mHeaderRightBtn = (ImageView) findViewById(R.id.header_right_btn);
		mHeaderRightBtn.setOnClickListener(this);
		mHeaderTitle = (TextView) findViewById(R.id.header_title_txt);
		mFragmentManager = getSupportFragmentManager();
		mTitleStack = new Stack<String>();
		mParseUser = ParseUser.getCurrentUser();
		addFragmentToStack();
	}

	public void pushTitle(String title) {
		if (mTitleStack != null) {

			mTitleStack.push(title);
		}
	}

	public String popTitle() {
		if (mTitleStack.size() > 0) {

			return mTitleStack.pop();
		}

		return "";
	}

	public void setActivityTitle(String title) {
		if (mHeaderTitle != null) {

			mHeaderTitle.setText(title);
		}
	}

	public void setNavigationImage(int headerLeftResId, int headerRightResId) {
		if (mHeaderLeftBtn != null && headerLeftResId != Integer.MAX_VALUE) {

			mHeaderLeftBtn.setImageResource(headerLeftResId);
		} else {

			mHeaderLeftBtn.setImageResource(0);
		}

		if (mHeaderRightBtn != null && headerRightResId != Integer.MAX_VALUE) {

			mHeaderRightBtn.setImageResource(headerRightResId);
		} else {

			mHeaderRightBtn.setImageResource(0);
		}
	}

	private void initViews(int count) {
		if (count == 0) {

			mHeaderLeftBtn.setVisibility(View.GONE);
		} else {

			mHeaderLeftBtn.setVisibility(View.VISIBLE);
		}
	}

	private int getStackCount() {

		int count = mFragmentManager.getBackStackEntryCount();
		return count;
	}

	private void removeFragmentFromStack() {
		if (getStackCount() - 1 == 1) {

			mHeaderLeftBtn.setVisibility(View.GONE);
		}

		if (mFragmentManager.getBackStackEntryCount() > 1) {

			mFragmentManager.popBackStack();
		} else {

			finish();
		}
	}

	private void addFragmentToStack() {

		int count = getStackCount();
		initViews(count);
		if (count == 0) {

			newFragment = InterestFragment.create(HobbyBaseFragment.FIRST_MODE);
		} else if (count == 1) {

			newFragment = SportsFragment.create(HobbyBaseFragment.FIRST_MODE);
		} else if (count == 2) {

			newFragment = FilmFragment.create(HobbyBaseFragment.FIRST_MODE);
		} else if (count == 3) {

			newFragment = MusicFragment.create(HobbyBaseFragment.FIRST_MODE);
		} else if (count == 4) {

			newFragment = UserSettingFragment.create();
		}

		ft = getSupportFragmentManager().beginTransaction();
		if (count != 0) {

			ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out,
					R.anim.slide_right_in, R.anim.slide_right_out);
		}

		ft.replace(R.id.fragment_initation_content, newFragment, FRAGMENT_TAG);
		ft.addToBackStack(null);
		ft.commit();
	}

	public void addFragment(Fragment fragment) {

		ft = getSupportFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out,
				R.anim.slide_right_in, R.anim.slide_right_out);
		ft.replace(R.id.fragment_initation_content, fragment, FRAGMENT_TAG);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_right_btn:

			LogUtil.e(this.getClass().getSimpleName(), "rightClick");
			saveData();
			addFragmentToStack();
			break;
		case R.id.header_left_btn:

			LogUtil.e(this.getClass().getSimpleName(), "BackClick");
			saveData();
			removeFragmentFromStack();
			break;
		default:
			break;
		}
	}

	private void saveData() {
		Fragment fragment = getSupportFragmentManager().findFragmentByTag(
				FRAGMENT_TAG);
		if (fragment == null) {

			return;
		}

		if (fragment instanceof HobbyBaseFragment) {
			ArrayList<Boolean> arr = new ArrayList<Boolean>();
			ArrayList<HobbyModel> list = ((HobbyBaseFragment) fragment)
					.getSelecedArr();
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					if (!list.get(i).isEmpty) {

						arr.add(list.get(i).isSelected);
					}
				}

				if (mParseUser != null) {
					if (fragment instanceof InterestFragment) {

						mParseUser.put("INTEREST", arr);
						mParseUser.put("Status", false);
						try {

							ParseObject meInfo = mParseUser
									.getParseObject("MeInfo");
							ParseObject filter = mParseUser
									.getParseObject("Filter");
							meInfo.saveInBackground();
							filter.saveInBackground();
							mParseUser.saveInBackground();
						} catch (Exception e) {
							// TODO Auto-generated catch block

						}
					} else if (fragment instanceof SportsFragment) {

						mParseUser.put("SPORTS", arr);
					} else if (fragment instanceof FilmFragment) {

						mParseUser.put("FILMS", arr);
					} else if (fragment instanceof MusicFragment) {

						mParseUser.put("MUSIC", arr);
					} else if (fragment instanceof FriendForFragment) {

						ParseObject meInfo = (ParseObject) mParseUser
								.get("MeInfo");
						if (meInfo != null) {

							meInfo.put("Hobby", arr);
						}
					}
				}
			}
		} else if (fragment instanceof UserSettingFragment) {
			try {

				mParseUser.saveInBackground();
				Intent intent = new Intent(UserSettingInitialActivity.this,
						HomeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
	}

	@Override
	public void onBackPressed() {

		LogUtil.e(this.getClass().getSimpleName(), "onBackPressed");
		saveData();
		removeFragmentFromStack();
	}

	@Override
	public void onClickCheckHobies(int type, ArrayList<HobbyModel> selectedItems) {
		if (selectedItems != null) {
			ArrayList<Boolean> arr = new ArrayList<Boolean>();
			for (int i = 0; i < selectedItems.size(); i++) {
				if (!selectedItems.get(i).isEmpty) {

					arr.add(selectedItems.get(i).isSelected);
				}
			}

			if (type == Constant.INTEREST) {

				mParseUser.put("INTEREST", arr);
			} else if (type == Constant.SPORT) {

				mParseUser.put("SPORTS", arr);
			} else if (type == Constant.FILM) {

				mParseUser.put("FILMS", arr);
			} else if (type == Constant.MUSIC) {

				mParseUser.put("MUSIC", arr);
			} else if (type == Constant.HOBBY) {

				ParseObject meInfo = (ParseObject) mParseUser.get("MeInfo");
				if (meInfo != null) {

					meInfo.put("Hobby", arr);
				}
			}

			try {

				mParseUser.saveInBackground();
				addFragmentToStack();
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
	}

	@Override
	public void onClickSkip() {

		addFragmentToStack();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.darren.befriend.fragments.UniversityFragment.OnUniversitySelected
	 * #onUserniverSelected(int)
	 */
	@Override
	public void onUserniverSelected(int selectedUniver) {
		// TODO Auto-generated method stub
		ParseObject meInfo = (ParseObject) mParseUser.get("MeInfo");
		if (meInfo != null) {

			meInfo.put("University", selectedUniver);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.darren.befriend.BaseActivity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

	}

}
